package com.matt.repository.impl;

import com.matt.domain.Children;
import com.matt.foundation.DO.ChildrenListDO;
import com.matt.foundation.utils.DateUtil;
import com.matt.foundation.vo.DialogsVo;
import com.matt.repository.base.BaseNativeSqlRepository;
import com.matt.repository.base.UserChildrenRepository;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/7/18
 * *******************************
 */
@Service
public class UserChildrenRepositoryImpl extends BaseNativeSqlRepository implements UserChildrenRepository {

    @Override
    public List<Object[]> findByChildrenId(String childrenId) {
        String findTeacherSql=String.format("select u.id as id,uc.id as rid, u.name as name, u.phone as phone, u.company as company, u.email as email, uc.relation as relation,uc.remark as remark," +
                "uc.cameraEnabled, uc.wechatEnabled,uc.templateMsgEnabled, uc.teacherChatEnabled, u.province as province, u.city AS city, u.district AS district, u.detailedAddress AS detailedAddress, u.education AS education, u.position AS position"
                + " from users u LEFT JOIN userchildren uc ON u.id = uc.uid  where uc.cid = '%s' order by uc.createTime asc", childrenId);
        List<Object[]> objectArrayList = sqlArrayList(findTeacherSql);
        return objectArrayList;
    }

    @Override
    public List<ChildrenListDO> findNurseryChild(String userId) {
        String findSql=String.format("SELECT c.id AS id, c.NAME AS name, c.gender AS gender, c.imgUrl AS imgUrl, ci.nurseryStatus AS nurseryStatus, c.anxietyTime AS anxietyTime, " +
                "c.courseStartDate AS courseStartDate FROM userchildren uc, children c, children_extend_info ci WHERE uc.cid = c.id AND c.id = ci.childrenId AND uc.uid = '%s'", userId);
        List<ChildrenListDO> objectArrayList = sqlObjectList(findSql, ChildrenListDO.class);
        return objectArrayList;
    }

    @Override
    public List<String> getChildrenIdsByUserId(String userId) {
        String sql = String.format("SELECT DISTINCT(cid) from userchildren where uid = '%s'",userId);
        List<Map<String,String>> list = sqlObjectList(sql, HashMap.class);
        List<String> cid = list.stream().map(o -> o.get("cid")).collect(Collectors.toList());
        return cid;
    }

    @Override
    public List<Map<String,Object>> getChildrenByUserId(String userId) {
        String sql = String.format("SELECT c.* from userchildren u left JOIN children c on u.cid = c.id  where u.uid = '%s'",userId);
        List<Map<String,Object>> list = sqlObjectList(sql, HashMap.class);
        return list;
    }

    //根据班级Id 取班级内所有宝宝和家长信息 此处id 就是userId
    @Override
    public List<DialogsVo> findAllUsersByClassId(String classId) {
        String sql = String.format("SELECT u.uid as id,r.classesId,r.childrenId,c.name as childrenName,u.relation,c.imgUrl as childrenImg,c.gender," +
                "t.NAME AS userName,t.phone AS userPhone,cl.NAME AS className from rel_classes_children r" +
                " left join userchildren u on r.childrenId = u.cid RIGHT JOIN children c on r.childrenId = c.id " +
                "LEFT JOIN users t ON u.uid = t.id LEFT JOIN classes cl ON cl.id = r.classesId " +
                " where u.uid is not null " +
                " and r.classesId = '%s' ORDER BY r.classesId,c.NAME ASC;",classId);
        List<DialogsVo> list = sqlObjectList(sql, DialogsVo.class);
        return list;
    }

    //根据teacherId 取班级内所有宝宝和家长信息 此处id 就是userId
    @Override
    public List<DialogsVo> findAllUsersByClassIds(List<String> classIds) {
        if(classIds == null || classIds.size() == 0){
            return new ArrayList<>();
        }

        String sql = "SELECT u.uid as id,r.classesId,r.childrenId,c.name as childrenName,u.relation,c.imgUrl as childrenImg,c.gender," +
                "t.NAME AS userName,t.phone AS userPhone,cl.NAME AS className from rel_classes_children r" +
                " left join userchildren u on r.childrenId = u.cid RIGHT JOIN children c on r.childrenId = c.id " +
                "LEFT JOIN users t ON u.uid = t.id LEFT JOIN classes cl ON cl.id = r.classesId " +
                " where u.uid is not null " +
                " and r.classesId in (:1) ORDER BY r.classesId,c.NAME ASC;";

        List param = new ArrayList();
        param.add(classIds);
        List<DialogsVo> list = sqlObjectList(sql, param, DialogsVo.class);

        return list;
    }

    @Override
    public List<Children> findByChildrenBaseInfoByUserId(String uid) {
        String sql = String.format("SELECT c.* from userchildren u left JOIN children c on u.cid = c.id where uid = '%s' ",uid);
        List<Map<String,Object>> list =  sqlObjectList(sql,HashMap.class);
        List<Children> result = Lists.newArrayList();
        list.stream().forEach(m  -> {
            Children children = Children.of(
                    m.get("name").toString(),
                    null,
                    m.get("gender") == null ? null : Integer.valueOf( m.get("gender").toString()),
                    m.get("birthday") == null ? null : DateUtil.parseToDate(m.get("birthday").toString()),
                    m.get("aid").toString(),
                    m.get("address").toString(),
                    null,
                    m.get("staffId").toString());
            children.setId(m.get("id").toString());
            Object imgUrl = m.get("imgUrl");
            children.setImgUrl(imgUrl == null ? "" : imgUrl.toString());
            result.add(children);
        });

        return result;
    }
}
